const initialState = {
  allList: [] as ChannelListType[],
  channelList: [] as ChannelListType[],
};
export type ChannelListType = {
  id: number;
  name: string;
};
type ChannelAction =
  | {
      type: 'channel/getAllChannel';
      payload: ChannelListType[];
    }
  | {
      type: 'channel/getUserChgannel';
      payload: ChannelListType[];
    }
  | {
      type: 'channel/delChannel';
      payload: number;
    }
  | {
      type: 'channel/AddChgannel';
      payload: ChannelListType;
    };
export default function channelReducer(
  state = initialState,
  actions: ChannelAction
): typeof initialState {
  switch (actions.type) {
    case 'channel/getAllChannel':
      return { ...state, allList: actions.payload };
    case 'channel/getUserChgannel':
      return { ...state, channelList: actions.payload };
    case 'channel/AddChgannel':
      return { ...state, channelList: [...state.channelList, actions.payload] };
    case 'channel/delChannel':
      return {
        ...state,
        channelList: state.channelList.filter(
          (item) => item.id !== actions.payload
        ),
      };
    default:
      return state;
  }
}
